package com.example.service;


import com.example.common.JwtResponse;
import com.example.common.JwtTokenUtil;
import com.example.common.support.CustomException;
import com.example.common.support.CustomExceptionType;
import com.example.function.sys.model.one.SysUser;
import com.example.function.sys.model.pojo.SysUserPojo;
import com.example.function.sys.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class JwtAuthService {

    @Autowired
    ISysUserService sysUserService;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Resource
    JwtTokenUtil jwtTokenUtil;

    /**
     * 登录认证换取JWT令牌
     *
     * @return JwtResponse
     */
    public JwtResponse login(String loginName, String password, Integer userType) throws CustomException {

        //验证用户名密码是否有效
        SysUser sysUser = sysUserService.findByLoginNameAndUserType(loginName, userType);

        //查询不到用户或者密码不匹配，或者用户被禁用
        if (sysUser == null || !passwordEncoder.matches(password, sysUser.getUserLoginPassword()) || !(sysUser.getUserStatus() == 1)) {
            throw new CustomException(CustomExceptionType.USER_INPUT_ERROR
                    , "用户名或者密码不正确");
        }

        //生成jwt返回
        String token = jwtTokenUtil.generateToken(loginName);

        JwtResponse jwtResponse = new JwtResponse();
        jwtResponse.setToken(token);
        jwtResponse.setExpiration(jwtTokenUtil.getExpirationDateFromToken(token));
        jwtResponse.setUser(SysUserPojo.wrapper(sysUser));

        return jwtResponse;
    }


    /**
     * 刷新JWT令牌
     *
     * @return JwtResponse
     */
    public JwtResponse refreshToken(String oldToken) {

        String token = jwtTokenUtil.refreshToken(oldToken);
        if (token == null) {
            throw new CustomException(CustomExceptionType.USER_INPUT_ERROR
                    , "token过期或无效，请重新获取token");
        }
        final String loginName = jwtTokenUtil.getUsernameFromToken(token);
        final SysUser sysUser = sysUserService.findByLoginName(loginName);

        //查询不到用户，或者用户被禁用
        if (sysUser == null || !(sysUser.getUserStatus() == 1)) {
            throw new CustomException(CustomExceptionType.USER_INPUT_ERROR
                    , "用户不存在或被禁用");
        }

        JwtResponse jwtResponse = new JwtResponse();
        jwtResponse.setToken(token);
        jwtResponse.setExpiration(jwtTokenUtil.getExpirationDateFromToken(token));
        jwtResponse.setUser(SysUserPojo.wrapper(sysUser));
        return jwtResponse;

    }

    /**
     * 验证token是否到期
    */
    public JwtResponse.ValidateResponse validateToken(String token) {
        final Date expirationDate = jwtTokenUtil.getExpirationDateFromToken(token);
        if(expirationDate == null){
            throw new CustomException(CustomExceptionType.USER_INPUT_ERROR
                    , "无效token，请重新获取token");
        }
        final Boolean tokenExpired = jwtTokenUtil.isTokenExpired(token);
        return new JwtResponse.ValidateResponse(tokenExpired, expirationDate);
    }


}
